home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / OS Utilities / TimeZone.Daylight / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.2 KB  |  287 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        main.c
  3.  
  4.     Contains:    draws corrent location and time zone information.            
  5.                 shows  a function that determines if Daylight                
  6.                 savings time is turned on or off from the Date & Time        
  7.                 control panel.  Inside Mac: OS Utililies Chapter 4             
  8.                 should be consulted for further information.
  9.  
  10.     Written by: David Hayward    
  11.  
  12.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 7/23/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 
  25.  
  26. */
  27. /****************************************************************************/
  28. #include <TextUtils.h>
  29. #include <Dialogs.h>
  30. #include <Fonts.h>
  31. #include <Icons.h>
  32. #include <Memory.h>
  33. #include <Types.h>
  34. #include <ToolUtils.h>
  35. #include <Windows.h>
  36. #include <FixMath.h>
  37. #include <QuickDraw.h>
  38. #include <GestaltEqu.h>
  39. #include <OSUtils.h>
  40. #include "initMac.h"
  41.  
  42.  
  43. /*\
  44. |*| ---------------------------------------------------------------------
  45. |*| GLOBALS
  46. |*| ---------------------------------------------------------------------
  47. \*/
  48. WindowPtr        gWindow;
  49. MachineLocation    gLocation;
  50. Boolean            done=0;
  51.  
  52.  
  53. /*\
  54. |*| ---------------------------------------------------------------------
  55. |*| PROTOTYPES
  56. |*| ---------------------------------------------------------------------
  57. \*/
  58. void main                (void);
  59. void createWindow        (void);
  60. void DoEventLoop        (void);
  61. void DoUpdate            (WindowPtr whichWindow);
  62. void DoMouseDown        (EventRecord event);
  63. void DrawHMS (long time);
  64. void DrawDMS (long degr);
  65. int IsDaylightSavingsOn();
  66.  
  67. /*\
  68. |*| ---------------------------------------------------------------------
  69. |*| Determine if Daylight Savings Time is on
  70. |*| ---------------------------------------------------------------------
  71. \*/
  72.  
  73. int IsDaylightSavingsOn()
  74. {
  75.     int retVal = 0;
  76.     MachineLocation    theLocation;
  77.  
  78.     
  79.     ReadLocation(&theLocation);
  80.     if (theLocation.u.dlsDelta == (signed char) 0x80) {
  81.         retVal = 1;
  82.     }
  83.     return(retVal);
  84. }        
  85.             
  86.  
  87.  
  88. void main (void)
  89. {
  90.     InitToolBox(1);
  91.  
  92.     createWindow();
  93.     
  94. ReadLocation(&gLocation);
  95.     
  96.     DoEventLoop();
  97. }
  98.  
  99.  
  100. void createWindow (void)
  101. {
  102.     gWindow = GetNewCWindow(128,nil,nil);
  103.     SetPort( gWindow );
  104. }
  105.  
  106.  
  107. /*\
  108. |*| ---------------------------------------------------------------------
  109. |*| DoEventLoop()
  110. |*| ---------------------------------------------------------------------
  111. \*/
  112. void DoEventLoop (void)
  113. {
  114.     EventRecord event;
  115.     long        mssg;
  116.  
  117.     while ( !done ) 
  118.     {
  119.         if (WaitNextEvent( everyEvent, &event, 60, nil ))
  120.         {
  121.             mssg = event.message;
  122.             switch (event.what)
  123.             {
  124.                 case mouseDown :                        /* handle mouse clicks */
  125.                     DoMouseDown (event);
  126.                     break;
  127.                 
  128.                 case keyDown :                            /* handle key hits */
  129.                 case autoKey :
  130.                     break;
  131.                     
  132.                 case updateEvt :                        /* handle update events */
  133.                     DoUpdate((WindowPtr)mssg);
  134.                     break;
  135.                 
  136.                 case osEvt:                                /* handle os events */
  137.                     if ( (mssg>>24)                        /* if high byte of message indicates */
  138.                           == suspendResumeMessage )        /* this is suspend/resume event */
  139.                     {
  140.                         if (mssg & resumeFlag)            /* if resume event */
  141.                         {
  142.                             /* we're switching back from another app so */
  143.                             /* we may need to show floating window */
  144.                         //    InvalRect(&((GrafPtr)gWindow)->portRect);
  145.                         }
  146.                     }
  147.                     break;
  148.             }
  149.         }
  150.         else            /* null event */
  151.         {
  152.             MachineLocation temp;
  153.             
  154.             ReadLocation(&temp);
  155.             
  156.             if ( (gLocation.latitude != temp.latitude) ||
  157.                  (gLocation.longitude != temp.longitude) ||
  158.                  (gLocation.u.gmtDelta != temp.u.gmtDelta) )
  159.             {
  160.                 gLocation = temp;
  161.                 InvalRect(&((GrafPtr)gWindow)->portRect);
  162.             }
  163.         }
  164.     }
  165. }
  166.  
  167.  
  168. /*\
  169. |*| ---------------------------------------------------------------------
  170. |*| DoMouseDown
  171. |*| handle DoMouseDown events
  172. |*| ---------------------------------------------------------------------
  173. \*/
  174. void DoMouseDown (EventRecord event)
  175. {
  176.     WindowPtr   window;
  177.     short       clickArea;
  178.     Rect        screenRect;
  179.  
  180.     clickArea = FindWindow( event.where, &window );
  181.     
  182.     switch (clickArea)
  183.     {
  184.         case inDrag:
  185.             screenRect = (**GetGrayRgn()).rgnBBox;
  186.             DragWindow( window, event.where, &screenRect );
  187.             break;
  188.  
  189.         case inContent:
  190.             if (window != FrontWindow())
  191.                 SelectWindow( window );
  192.             break;
  193.         
  194.         case inGoAway:
  195.             if (TrackGoAway( window, event.where ))
  196.                 done = 1;
  197.             break;
  198.     }
  199. }
  200.             
  201.             
  202. /*\
  203. |*| ---------------------------------------------------------------------
  204. |*| DoUpdate
  205. |*| handle update events
  206. |*| ---------------------------------------------------------------------
  207. \*/
  208. void DoUpdate (WindowPtr window)
  209. {
  210.     Str255    str;
  211.     long    gmt;
  212.     
  213.     SetPort( window );
  214.     BeginUpdate( window );
  215.  
  216.     EraseRect( &((GrafPtr)window)->portRect);
  217.     
  218.     MoveTo(10,20);
  219.     DrawString("\pLat: ");
  220.     DrawDMS((long)gLocation.latitude);
  221.     
  222.     MoveTo(10,40);
  223.     DrawString("\pLong: ");
  224.     DrawDMS((long)gLocation.longitude);
  225.     
  226.     MoveTo(10,60);
  227.     DrawString("\pdlsΔ: ");
  228.     NumToString( (long)gLocation.u.dlsDelta, str);
  229.     DrawString(str);
  230.     
  231.     if (IsDaylightSavingsOn()) {
  232.         DrawString("\p Daylight On ");
  233.     } else {
  234.         DrawString("\p Daylight Off ");
  235.     }
  236.     
  237.     MoveTo(10,80);
  238.     DrawString("\pgmtΔ: ");
  239.     gmt = gLocation.u.gmtDelta;
  240.     gmt &= 0x00FFFFFF;            // mask off dlsDelta
  241.     if (gmt & 0x00800000)        // if sign bit of gmtDelta is set
  242.         gmt |= 0xFF000000;        // then turn on high char if long
  243.     DrawHMS (gmt);
  244.  
  245.     EndUpdate( window );
  246. }
  247.  
  248.  
  249. void DrawHMS (long time)
  250. {
  251.     Str255    str;
  252.  
  253.     if (time<0)
  254.     {
  255.         time = -time;
  256.         DrawString("\p- ");
  257.     }
  258.  
  259.     NumToString( time / 3600L, str);
  260.     DrawString(str);
  261.     DrawString("\ph ");
  262.     
  263.     NumToString( (time % 3600L) / 60L, str);
  264.     DrawString(str);
  265.     DrawString("\pm ");
  266.  
  267. }
  268.  
  269.  
  270. void DrawDMS (long degr)
  271. {
  272.     Str255    str;
  273.     
  274.     if (degr<0)
  275.     {
  276.         degr = -degr;
  277.         DrawString("\p- ");
  278.     }
  279.  
  280.     NumToString( degr / 11930464L, str);
  281.     DrawString(str);
  282.     DrawString("\p° ");
  283.     
  284.     NumToString( (degr % 11930464L) / 198841L, str);
  285.     DrawString(str);
  286.     DrawString("\pm ");
  287. }